#include "FileDataStream.h"

namespace DreamEngine
{
	CFileDataStream::CFileDataStream(void):
m_size(0)
{
}


CFileDataStream::~CFileDataStream(void)
{
	Close();
}


bool CFileDataStream::Open(const std::string& fileName, AccessMode mode/* =READ */)
{
	m_fileName = fileName;

	m_fstream.open(m_fileName.c_str(), mode);
	if (!m_fstream.is_open())return false;

	m_fstream.seekg(0, END);
	m_size = (int)m_fstream.tellg();
	m_fstream.seekg(0, BEGIN);

	return true;
}

bool CFileDataStream::Close()
{
	if (m_fstream.is_open())
	{
		m_fstream.close();
	}
	return true;
}

bool CFileDataStream::IsOpen()const
{
	return m_fstream.is_open();
}

bool CFileDataStream::Read(void* pBuffer, int dateSize)
{
	if (IsEnd() || pBuffer==NULL)
		return false;

	m_fstream.read((char*)(pBuffer), dateSize);
	return true;
}

void CFileDataStream::Write(const void* pBuffer, int dateSize)
{
	if (pBuffer==NULL)return;

	m_fstream << pBuffer;
	//m_fstream.write((const char*)(pBuffer), dateSize);
}

void CFileDataStream::MoveToBegin()
{
	m_fstream.seekg(0, BEGIN); 
}

void CFileDataStream::MoveToEnd()
{
	m_fstream.seekp(0, END);
}

bool CFileDataStream::IsEnd()
{
	return m_fstream.tellg()==(std::fstream::pos_type)(END);
}

int CFileDataStream::GetOffset(int relatPos/* =0 */)
{
	int cur = int(m_fstream.tellg());

	return (cur-relatPos);
}

void CFileDataStream::GetAsString(std::string& str)
{
	char* buffer = new char[m_size+1];

	m_fstream.read(buffer, m_size);
	str.assign(buffer, m_size);

	delete []buffer;
}

std::string CFileDataStream::GetAsString(void)
{
	char* buffer = new char[m_size+1];

	std::string str;
	m_fstream.read(buffer, m_size);
	str.assign(buffer, m_size);

	delete []buffer;


	return str;
}

}

